package com.cellulam.framework.core.exceptions;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.sound.midi.SysexMessage;
import java.util.Collection;
import java.util.concurrent.Callable;

public final class AssertTools {
    public static final void notNull(Object object) {
        notNull(object, SystemErrorStatus.RESOURCE_NOT_EXIST);
    }

    public static final void notNull(Object object, ErrorStatus errorStatus) {
        if (object == null) {
            throw new BusinessException(errorStatus);
        }
    }

    public static final void notNullOrEmpty(Collection collection, ErrorStatus errorStatus) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(errorStatus);
        }
    }

    public static final void notNullOrEmpty(String value, ErrorStatus errorStatus) {
        if (StringUtils.isEmpty(value)) {
            throw new BusinessException(errorStatus);
        }
    }

    public static final void isTrue(boolean condition, ErrorStatus errorStatus) {
        if (!condition) {
            throw new BusinessException(errorStatus);
        }
    }

    public static final void isFalse(boolean condition, ErrorStatus errorStatus) {
        if (condition) {
            throw new BusinessException(errorStatus);
        }
    }

    public static final void predicate(Callable<Boolean> predicate, ErrorStatus errorStatus) {
        try {
            if (!predicate.call()) {
                throw new BusinessException(errorStatus);
            }
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new SysException(e);
        }
    }

    public static final void notNull(Object object, String message) {
        if (object == null) {
            throw new SysException(message);
        }
    }

    public static final void notNull(Object object, int status, String message) {
        if (object == null) {
            throw new BusinessException(status, message);
        }
    }

    public static final void notNullOrEmpty(Collection collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new SysException(message);
        }
    }

    public static final void notNullOrEmpty(Collection collection, int status, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(status, message);
        }
    }

    public static final void notNullOrEmpty(String value, String message) {
        if (StringUtils.isEmpty(value)) {
            throw new SysException(message);
        }
    }

    public static final void notNullOrEmpty(String value, int status, String message) {
        if (StringUtils.isEmpty(value)) {
            throw new BusinessException(status, message);
        }
    }

    public static final void isTrue(boolean condition, String message) {
        if (!condition) {
            throw new SysException(message);
        }
    }

    public static final void isTrue(boolean condition, int status, String message) {
        if (!condition) {
            throw new BusinessException(status, message);
        }
    }

    public static final void isFalse(boolean condition, String message) {
        if (condition) {
            throw new SysException(message);
        }
    }

    public static final void isFalse(boolean condition, int status, String message) {
        if (condition) {
            throw new BusinessException(status, message);
        }
    }

    public static final void predicate(Callable<Boolean> predicate, String message) {
        try {
            if (!predicate.call()) {
                throw new SysException(message);
            }
        } catch (BusinessException e) {
            throw e;
        } catch (SysException e) {
            throw e;
        } catch (Exception e) {
            throw new SysException(e);
        }
    }

    public static final void predicate(Callable<Boolean> predicate, int status, String message) {
        try {
            if (!predicate.call()) {
                throw new BusinessException(status, message);
            }
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new SysException(e);
        }
    }
}
